package com.zdkj.subway.support.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.constant.CommonConstants;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.annotation.Inner;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.support.dto.out.StationPersonGuardDTO;
import com.zdkj.subway.support.entity.SwPmStationPersonGuard;
import com.zdkj.subway.support.entity.SwPmStationPersonGuardRecord;
import com.zdkj.subway.support.service.SwPmStationPersonGuardRecordService;
import com.zdkj.subway.support.service.SwPmStationPersonGuardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * 车站人员在岗信息表
 *
 * @author cuilei
 * @date 2020-06-04 10:30:20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swpmstationpersonguard")
@Api(value = "swpmstationpersonguard", tags = "车站人员在岗信息表管理")
public class SwPmStationPersonGuardController {

    /**
     * 离岗
     */
    private final String LEAVEL = "0";
    /**
     * 在岗
     */
    private final String ON_DUTY = "1";
    private final SwPmStationPersonGuardService swPmStationPersonGuardService;
    private final SwPmStationPersonGuardRecordService swPmStationPersonGuardRecordService;

    /**
     * 分页查询
     *
     * @param page               分页对象
     * @param stationPersonGuard 车站人员在岗信息表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwPmStationPersonGuardPage(Page page, StationPersonGuardDTO stationPersonGuard) {
        return R.ok(swPmStationPersonGuardService.pageSelect(page, stationPersonGuard));
    }

    @ApiOperation(value = "添加车站人员记录", notes = "添加车站人员记录")
    @PostMapping()
    public R save(SwPmStationPersonGuard swPmStationPersonGuard) {
        return R.ok(swPmStationPersonGuardService.save(swPmStationPersonGuard));
    }

    @ApiOperation(value = "添加车站人员记录", notes = "添加车站人员记录")
    @Inner
    @PostMapping("/inner")
    public R saveInner(@RequestBody SwPmStationPersonGuard swPmStationPersonGuard) {
        return R.ok(swPmStationPersonGuardService.save(swPmStationPersonGuard));
    }

    /**
     * 通过id查询车站人员在岗信息表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swPmStationPersonGuardService.getById(id));
    }


    @ApiOperation(value = "每天自动添加车站人员在岗记录", notes = "每天自动添加车站人员在岗记录")
    @Inner
    @PostMapping("/saveDaiyRecord")
    public R saveDaiyPersonRecord() {
        return R.ok(swPmStationPersonGuardService.saveDaiyPersonRecord());
    }

    @ApiOperation(value = "通过userCode设置车站人员在岗离岗状态", notes = "flag:在岗true,离岗false")
    @SysLog("通过userCode设置车站人员在岗离岗状态")
    @PutMapping("/{userCode}/{flag}")
    public R changeWorkRecord(@PathVariable("userCode") String userCode, @PathVariable("flag") Boolean flag) {
        SwPmStationPersonGuard swPmStationPersonGuard = swPmStationPersonGuardService.getOne(
                Wrappers.<SwPmStationPersonGuard>query().lambda()
                        .eq(SwPmStationPersonGuard::getUserCode, userCode));
        if (null == swPmStationPersonGuard) {
            return R.failed("未找到该人员记录");
        }
        swPmStationPersonGuard.setGuardStatus(LEAVEL);
        swPmStationPersonGuard.setOperationTime(LocalDateTime.now());
        swPmStationPersonGuard.setDelFlag(CommonConstants.STATUS_NORMAL);
        if (flag) {
            swPmStationPersonGuard.setGuardStatus(ON_DUTY);
        }
        swPmStationPersonGuardService.updateById(swPmStationPersonGuard);
        //记录状态
        SwPmStationPersonGuardRecord swPmStationPersonGuardRecord =
                ObjectUtils.getDefaultObj(SwPmStationPersonGuardRecord.class);
        BeanUtils.copyProperties(swPmStationPersonGuard, swPmStationPersonGuardRecord);
        swPmStationPersonGuardRecord.setId(null);
        swPmStationPersonGuardRecordService.save(swPmStationPersonGuardRecord);
        return R.ok();
    }
}
